<?php
  global $mosUser;
  defined( 'joscom' ) or die( 'Direct Access to this location is not allowed.' );
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require('components/'.$option.'/includes/application_top.php');
  }
 
  if (isset($_REQUEST['guest']) && $cart->count_contents() < 1) tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  require(DIR_WS_LANGUAGES . $language . '/' . $osMod . '.php');
	if (USE_JOOMLA_CB == 'true'){
	if ($mosUser['id'] == '') {
	if ($_REQUEST['guest'] != 'guest'){
		tep_redirect($mosConfig_live_site.'/index.php?option=com_comprofiler&task=registers');
		}
		}
		}
  $process = false;
  if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'process')) {
    $process = true;

    if (ACCOUNT_GENDER == 'true') {
      if (isset($_REQUEST['gender'])) {
        $gender = tep_db_prepare_input($_REQUEST['gender']);
      } else {
        $gender = false;
      }
    }
    $firstname = tep_db_prepare_input($_REQUEST['firstname']);
    $lastname = tep_db_prepare_input($_REQUEST['lastname']);
       if (ACCOUNT_DOB == 'true') $dob = tep_db_prepare_input($_REQUEST['dob_month'] . '/' . $_REQUEST['dob_day'] . '/' . $_REQUEST['dob_year']);
    $email_address = tep_db_prepare_input($_REQUEST['email_address']);
    if (ACCOUNT_COMPANY == 'true') {
     $company = tep_db_prepare_input($_REQUEST['company']);
     $company_tax_id = tep_db_prepare_input($_REQUEST['company_tax_id']);
     }
    
    $street_address = tep_db_prepare_input($_REQUEST['street_address']);
    if (ACCOUNT_SUBURB == 'true') $suburb = tep_db_prepare_input($_REQUEST['suburb']);
    $postcode = tep_db_prepare_input($_REQUEST['postcode']);
    $city = tep_db_prepare_input($_REQUEST['city']);
    if (ACCOUNT_STATE == 'true') {
      $state = tep_db_prepare_input($_REQUEST['state']);
      if (isset($_REQUEST['zone_id'])) {
        $zone_id = tep_db_prepare_input($_REQUEST['zone_id']);
      } else {
        $zone_id = false;
      }
    }
    $country = tep_db_prepare_input($_REQUEST['country']);
   
    $telephone = tep_db_prepare_input($_REQUEST['telephone']);
    $fax = tep_db_prepare_input($_REQUEST['fax']);
    if (isset($_REQUEST['newsletter'])) {
      $newsletter = tep_db_prepare_input($_REQUEST['newsletter']);
    } else {
      $newsletter = false;
    }
    
    // Start Added by TEC of SonicFog.com
    // This is part one of two for the auto login hack
    $username = tep_db_prepare_input($_REQUEST['username']);
    $password = tep_db_prepare_input($_REQUEST['password']);
    $confirmation = tep_db_prepare_input($_REQUEST['confirmation']);
    $raw_password = $password;


    $error = false;

    if (ACCOUNT_GENDER == 'true') {
      if ( ($gender != 'm') && ($gender != 'f') ) {
        $error = true;
        $messageStack->add('create_account', ENTRY_GENDER_ERROR);
      }
    }

    if (strlen($firstname) < ENTRY_FIRST_NAME_MIN_LENGTH) {
      $error = true;
      $messageStack->add('create_account', ENTRY_FIRST_NAME_ERROR);
    }

    if (strlen($lastname) < ENTRY_LAST_NAME_MIN_LENGTH) {
      $error = true;
      $messageStack->add('create_account', ENTRY_LAST_NAME_ERROR);
    }

    if (ACCOUNT_DOB == 'true') {
      if (checkdate(substr(tep_date_raw($dob), 4, 2), substr(tep_date_raw($dob), 6, 2), substr(tep_date_raw($dob), 0, 4)) == false) {
        $error = true;
        $messageStack->add('create_account', ENTRY_DATE_OF_BIRTH_ERROR);
      }
    }

    if (strlen($email_address) < ENTRY_EMAIL_ADDRESS_MIN_LENGTH) {
      $error = true;
      $messageStack->add('create_account', ENTRY_EMAIL_ADDRESS_ERROR);
    } elseif (tep_validate_email($email_address) == false) {
      $error = true;
      $messageStack->add('create_account', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
    } else {
      $check_email_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "'");
      $check_email = tep_db_fetch_array($check_email_query);
      global $my;
      if ($check_email['total'] > 0 && !$my->id) {
        $error = true;
        $messageStack->add('create_account', ENTRY_EMAIL_ADDRESS_ERROR_EXISTS);
      }
    }

    if (strlen($street_address) < ENTRY_STREET_ADDRESS_MIN_LENGTH) {
      $error = true;
      $messageStack->add('create_account', ENTRY_STREET_ADDRESS_ERROR);
    }

    if (strlen($postcode) < ENTRY_POSTCODE_MIN_LENGTH) {
      $error = true;
      $messageStack->add('create_account', ENTRY_POST_CODE_ERROR);
    }

    if (strlen($city) < ENTRY_CITY_MIN_LENGTH) {
      $error = true;
      $messageStack->add('create_account', ENTRY_CITY_ERROR);
    }

    if (is_numeric($country) == false) {
      $error = true;
      $messageStack->add('create_account', ENTRY_COUNTRY_ERROR);
    }

    if (ACCOUNT_STATE == 'true') {
      $zone_id = 0;
      $check_query = tep_db_query("select count(*) as total from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "'");
      $check = tep_db_fetch_array($check_query);
      $entry_state_has_zones = ($check['total'] > 0);
      if ($entry_state_has_zones == true) {
        
        $zone_query = tep_db_query("select distinct zone_id from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' and (zone_name = '" . tep_db_input($state) . "' or zone_code = '" . tep_db_input($state) . "')");
		if (tep_db_num_rows($zone_query) == 1) {
          $zone = tep_db_fetch_array($zone_query);
          $zone_id = $zone['zone_id'];
        } else {
          $error = true;
          $messageStack->add('create_account', ENTRY_STATE_ERROR_SELECT);
        }
      } else {
        if (strlen($state) < ENTRY_STATE_MIN_LENGTH) {
          $error = true;
          $messageStack->add('create_account', ENTRY_STATE_ERROR);
        }
      }
    }

    if (strlen($telephone) < ENTRY_TELEPHONE_MIN_LENGTH) {
      $error = true;
      $messageStack->add('create_account', ENTRY_TELEPHONE_NUMBER_ERROR);
    }

    if (!$mosUser['validated']) {
    if (!isset($_GET['guest']) && !isset($_POST['guest'])) {
      if (strlen($password) < ENTRY_PASSWORD_MIN_LENGTH) {
        $error = true;
        $messageStack->add('create_account', ENTRY_PASSWORD_ERROR);
      } elseif ($password != $confirmation) {
        $error = true;
        $messageStack->add('create_account', ENTRY_PASSWORD_ERROR_NOT_MATCHING);
      }
      $password = md5($password);
      }
    } else {
      $password = $mosUser['password'];
    }

  $extra_fields_query = tep_db_query("select ce.fields_id, ce.fields_input_type, ce.fields_required_status, cei.fields_name, ce.fields_status, ce.fields_input_type, ce.fields_size from " . TABLE_EXTRA_FIELDS . " ce, " . TABLE_EXTRA_FIELDS_INFO . " cei where ce.fields_status=1 and ce.fields_required_status=1 and cei.fields_id=ce.fields_id and cei.languages_id =" . $languages_id);
   while($extra_fields = tep_db_fetch_array($extra_fields_query)){
    if(strlen($_REQUEST['fields_' . $extra_fields['fields_id']])<$extra_fields['fields_size']){
      $error = true;
      $string_error=sprintf(ENTRY_EXTRA_FIELDS_ERROR,$extra_fields['fields_name'],$extra_fields['fields_size']);
      $messageStack->add('create_account', $string_error);
    }
  }



    if ($error == false) {
      if (!$mosUser['validated']) {
       if (!isset($_GET['guest']) && !isset($_POST['guest'])) {
        $_POST['name']      = $_REQUEST['firstname'] . ' ' . $_REQUEST['lastname'];
        $_POST['username']  = $_REQUEST['username'];
        $_POST['email']     = $_REQUEST['email_address'];
        
        if(class_exists('JRequest')){
        $mos_user_result   = j15_create_user_account();
        }else{
        $mos_user_result    = mos_create_user_account();
        }
        
        
        if (!$mos_user_result) {
          // User Created but not validated
        } elseif ($mos_user_result == '1') {
          // User Created and validated
        } elseif ($mos_user_result) {
          $error = true;
          $messageStack->add('create_account', $mos_user_result);
        } else {
          $error = true;
          $messageStack->add('create_account', ENTRY_USERNAME_ERROR);
        }
      }
    }
    }

    if ($error == false) {

      if (!$mosUser['id'] && !isset($_REQUEST['guest']))  {
      
      if (!isset($_GET['guest']) && !isset($_POST['guest'])) {
      
        $messageStack->add('create_account', MOS_USER_VALIDATE_ERROR);
            }
            
      } else {
      
        $sql_data_array = array('customers_id' => $mosUser['id'],
                                'customers_firstname' => $firstname,
                                'customers_lastname' => $lastname,
                                'customers_email_address' => $email_address,
                                'customers_telephone' => $telephone,
                                'customers_fax' => $fax,
                                'customers_newsletter' => $newsletter,
                                'customers_password' => $password);

        if (ACCOUNT_GENDER == 'true') $sql_data_array['customers_gender'] = $gender;
        if (ACCOUNT_DOB == 'true') $sql_data_array['customers_dob'] = tep_date_raw($dob);
        
	  if ( ACCOUNT_COMPANY == 'true' && tep_not_null($company_tax_id)  ) {
      $sql_data_array['customers_group_ra'] = '1';
      }
      // EOF Separate Pricing Per Customer

// PWA BOF
      if ((isset($_GET['guest'])) or (isset($_POST['guest'])) && (defined('PURCHASE_WITHOUT_ACCOUNT') && (PURCHASE_WITHOUT_ACCOUNT == 'ja' || PURCHASE_WITHOUT_ACCOUNT == 'yes'))) {
        $pwa_array_customer = $sql_data_array;
        $customer_id = 0;
        tep_session_register('pwa_array_customer');
       
      } else {
      
      

        tep_db_perform(TABLE_CUSTOMERS, $sql_data_array, 'insert ignore');
        if($mosUser['id']){
        $customer_id = $mosUser['id'];
        }else{
        
        $customer_id = tep_db_insert_id();
        }
        
        //1197
        
         $customers_id = (int)$customer_id;
   	  	$extra_fields_query = tep_db_query("select ce.fields_id from " . TABLE_EXTRA_FIELDS . " ce where ce.fields_status=1 ");
    	  while($extra_fields = tep_db_fetch_array($extra_fields_query))
				{
				  if(isset($_REQUEST['fields_' . $extra_fields['fields_id']])){
            $sql_data_array = array('customers_id' => (int)$customers_id,
                              'fields_id' => $extra_fields['fields_id'],
                              'value' => $HTTP_POST_VARS['fields_' . $extra_fields['fields_id']]);
       		}
       		else
					{
					  $sql_data_array = array('customers_id' => (int)$customers_id,
                              'fields_id' => $extra_fields['fields_id'],
                              'value' => '');
						$is_add = false;
						for($i = 1; $i <= $_REQUEST['fields_' . $extra_fields['fields_id'] . '_total']; $i++)
						{
							if(isset($_REQUEST['fields_' . $extra_fields['fields_id'] . '_' . $i]))
							{
							  if($is_add)
							  {
                  $sql_data_array['value'] .= "\n";
								}
								else
								{
                  $is_add = true;
								}
              	$sql_data_array['value'] .= $_REQUEST['fields_' . $extra_fields['fields_id'] . '_' . $i];
							}
						}
					}

					tep_db_perform(TABLE_CUSTOMERS_TO_EXTRA_FIELDS, $sql_data_array, 'insert ignore');
      	}
   
        
        
        //end 1197
}
        $sql_data_array = array('customers_id' => $customer_id,
                                'entry_firstname' => $firstname,
                                'entry_lastname' => $lastname,
                                'entry_street_address' => $street_address,
                                'entry_postcode' => $postcode,
                                'entry_city' => $city,
                                'entry_country_id' => $country);

        if (ACCOUNT_GENDER == 'true') $sql_data_array['entry_gender'] = $gender;
        if (ACCOUNT_COMPANY == 'true') { // BOF adapted for Separate Pricing Per Customer
      $sql_data_array['entry_company'] = $company;
      $sql_data_array['entry_company_tax_id'] = $company_tax_id;
      } // EOF adapted for Separate Pricing Per Customer
        if (ACCOUNT_SUBURB == 'true') $sql_data_array['entry_suburb'] = $suburb;
        if (ACCOUNT_STATE == 'true') {
          if ($zone_id > 0) {
            $sql_data_array['entry_zone_id'] = $zone_id;
            $sql_data_array['entry_state'] = '';
          } else {
            $sql_data_array['entry_zone_id'] = '0';
            $sql_data_array['entry_state'] = $state;
          }
        }
// PWA BOF
     if (isset($_GET['guest']) or isset($_POST['guest'])) {
       $pwa_array_address = $sql_data_array;
       tep_session_register('pwa_array_address');
       $address_id = 0;
       $ecjc_pwa = 1;
       tep_session_register('ecjc_pwa');
     } else {
// PWA EOF
        tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array, 'insert ignore');

        $address_id = tep_db_insert_id();

        tep_db_query("update " . TABLE_CUSTOMERS . " set customers_default_address_id = '" . (int)$address_id . "' where customers_id = '" . (int)$customer_id . "'");

        // Match User Data on MOS
        osc_match_mos_user( $mosUser['id'], 1 );
        tep_db_query("insert ignore into " . TABLE_CUSTOMERS_INFO . " (customers_info_id, customers_info_number_of_logons, customers_info_date_account_created) values ('" . (int)$customer_id . "', '0', now())");
}
        if (SESSION_RECREATE == 'True') {
          tep_session_recreate();
        }

        $customer_first_name = $firstname;
        $customer_default_address_id = $address_id;
        $customer_country_id = $country;
        $customer_zone_id = $zone_id;
        tep_session_register('customer_id');
        tep_session_register('customer_first_name');
        tep_session_register('customer_default_address_id');
        tep_session_register('customer_country_id');
        tep_session_register('customer_zone_id');
if (isset($_GET['guest']) or isset($_POST['guest'])) tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING));
        // restore cart contents
        $cart->restore_contents();
        // restore wishlist to sesssion V1.1.7
        $wishlist->restore_contents();

        // build the message content
        $name = $firstname . ' ' . $lastname;

        if (ACCOUNT_GENDER == 'true') {
           if ($gender == 'm') {
             $email_text = sprintf(EMAIL_GREET_MR, $lastname);
           } else {
             $email_text = sprintf(EMAIL_GREET_MS, $lastname);
           }
        } else {
          $email_text = sprintf(EMAIL_GREET_NONE, $firstname);
        }

       // $email_text .= EMAIL_WELCOME . EMAIL_TEXT . EMAIL_CONTACT . EMAIL_WARNING;
       // Points/Rewards system V2.00 BOF
      if (NEW_SIGNUP_POINT_AMOUNT > 0) {
        tep_add_welcome_points($customer_id);
        
        $points_account .= '<a href="' . tep_href_link(FILENAME_MY_POINTS, '', 'SSL') . '"><b><u>' . EMAIL_POINTS_ACCOUNT . '</u></b></a>.';
        $points_faq .= '<a href="' . tep_href_link(FILENAME_MY_POINTS_HELP, '', 'NONSSL') . '"><b><u>' . EMAIL_POINTS_FAQ . '</u></b></a>.';
	    $text_points = sprintf(EMAIL_WELCOME_POINTS , $points_account, number_format(NEW_SIGNUP_POINT_AMOUNT,POINTS_DECIMAL_PLACES), $currencies->format(tep_calc_shopping_pvalue(NEW_SIGNUP_POINT_AMOUNT)),$points_faq) ."\n\n";
      }
	    
//    $email_text .= EMAIL_WELCOME . EMAIL_TEXT .EMAIL_CONTACT . EMAIL_WARNING;
      $email_text .= EMAIL_WELCOME . EMAIL_TEXT . $text_points .EMAIL_CONTACT . EMAIL_WARNING;
      
// Points/Rewards system V2.00 EOF
       // CCGV CODE BLOCK ADDED  ******************************************************* BEGIN
  if (NEW_SIGNUP_GIFT_VOUCHER_AMOUNT > 0) {
    $coupon_code = create_coupon_code();
    $insert_query = tep_db_query("insert into " . TABLE_COUPONS . " (coupon_code, coupon_type, coupon_amount, date_created) values ('" . $coupon_code . "', 'G', '" . NEW_SIGNUP_GIFT_VOUCHER_AMOUNT . "', now())");
    $insert_id = tep_db_insert_id($insert_query);
    $insert_query = tep_db_query("insert into " . TABLE_COUPON_EMAIL_TRACK . " (coupon_id, customer_id_sent, sent_firstname, emailed_to, date_sent) values ('" . $insert_id ."', '0', 'Admin', '" . $email_address . "', now() )");

    $email_text .= sprintf(EMAIL_GV_INCENTIVE_HEADER, $currencies->format(NEW_SIGNUP_GIFT_VOUCHER_AMOUNT)) . "\n\n" .
                   sprintf(EMAIL_GV_REDEEM, $coupon_code) . "\n\n" .
                   EMAIL_GV_LINK . tep_href_link(FILENAME_GV_REDEEM, 'gv_no=' . $coupon_code,'NONSSL', false) .
                   "\n\n";
  }
  if (NEW_SIGNUP_DISCOUNT_COUPON != '') {
		$coupon_code = NEW_SIGNUP_DISCOUNT_COUPON;
    $coupon_query = tep_db_query("select * from " . TABLE_COUPONS . " where coupon_code = '" . $coupon_code . "'");
    $coupon = tep_db_fetch_array($coupon_query);
		$coupon_id = $coupon['coupon_id'];		
    $coupon_desc_query = tep_db_query("select * from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $coupon_id . "' and language_id = '" . (int)$languages_id . "'");
    $coupon_desc = tep_db_fetch_array($coupon_desc_query);
    $insert_query = tep_db_query("insert into " . TABLE_COUPON_EMAIL_TRACK . " (coupon_id, customer_id_sent, sent_firstname, emailed_to, date_sent) values ('" . $coupon_id ."', '0', 'Admin', '" . $email_address . "', now() )");
    $email_text .= EMAIL_COUPON_INCENTIVE_HEADER .  "\n" .
                   sprintf("%s", $coupon_desc['coupon_description']) ."\n\n" .
                   sprintf(EMAIL_COUPON_REDEEM, $coupon['coupon_code']) . "\n\n" .
                   "\n\n";



  }
// CCGV CODE BLOCK ADDED  ******************************************************* END


        tep_mail($name, $email_address, EMAIL_SUBJECT, $email_text, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
     
      if ( ACCOUNT_COMPANY == 'true' && tep_not_null($company_tax_id) ) {
      $alert_email_text = "Please note that " . $firstname . " " . $lastname . " of the company: " . $company . " has created an account.";
      tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, 'Company account created', $alert_email_text, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
      }

    global $mosConfig_useractivation;
global $my;
    
    if ($mosConfig_useractivation == "0" && !$my->id && _AUTO_LOGIN == true){
        if (function_exists('josSpoofValue')) {
            $validate = josSpoofValue(1);
        }

        global $mainframe;

        $_POST[$validate] = '1';
        $_POST['passwd'] = $_POST['password'];
             
             
$mainframe->login();
//die();

//ok here we still need to hit the j1.5 option!!!!
        
        
        if ($cart->count_contents() < 1) {
            // Appears to work Ok!
            tep_redirect(tep_href_link(FILENAME_CREATE_ACCOUNT_SUCCESS, '', 'SSL')); 
        } else {
            // Appears to work Ok!
            tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
        }
    } else {
        // Untested!
        tep_redirect(tep_href_link(FILENAME_CREATE_ACCOUNT_SUCCESS, '', 'SSL')); 
    }
    exit;
        
      }
    }
  } else {
  if (tep_session_is_registered('pwa_array_customer') && tep_session_is_registered('pwa_array_address')) {
    $gender = isset($pwa_array_customer['customers_gender'])?$pwa_array_customer['customers_gender']:'';
    $company = isset($pwa_array_address['entry_company'])? $pwa_array_address['entry_company']:'';
    $firstname = isset($pwa_array_customer['customers_firstname'])? $pwa_array_customer['customers_firstname']:'';
    $lastname = isset($pwa_array_customer['customers_lastname'])? $pwa_array_customer['customers_lastname']:'';
    $dob = isset($pwa_array_customer['customers_dob'])? substr($pwa_array_customer['customers_dob'],-2).'.'.substr($pwa_array_customer['customers_dob'],4,2).'.'.substr($pwa_array_customer['customers_dob'],0,4):'';
    $email_address = isset($pwa_array_customer['customers_email_address'])? $pwa_array_customer['customers_email_address']:'';
    $street_address = isset($pwa_array_address['entry_street_address'])? $pwa_array_address['entry_street_address']:'';
    $suburb = isset($pwa_array_address['entry_suburb'])? $pwa_array_address['entry_suburb']:'';
    $postcode = isset($pwa_array_address['entry_postcode'])? $pwa_array_address['entry_postcode']:'';
    $city = isset($pwa_array_address['entry_city'])? $pwa_array_address['entry_city']:'';
    $state = isset($pwa_array_address['entry_state'])? $pwa_array_address['entry_state']:'0';
    $country = isset($pwa_array_address['entry_country_id'])? $pwa_array_address['entry_country_id']:'';
    $telephone = isset($pwa_array_customer['customers_telephone'])? $pwa_array_customer['customers_telephone']:'';
    $fax = isset($pwa_array_customer['customers_fax'])? $pwa_array_customer['customers_fax']:'';
     
  }
    // Prepare for Blank Form
    $GLOBALS['newsletter']  = 'on';
    
    // Separate the mosUser name into First / Last names
    global $my;
    if($my->name){
      $fName = preg_replace('/^(.+?)\s.*$/','$1',$my->name);
      if ($fName != $mosUser['name']) {
        $lName = preg_replace('/^.+?\s(.+)$/','$1',$my->name);
        $lName = preg_replace('/^\s*(.+)\s*/','$1',$lName);
      };
    };
    $GLOBALS['firstname']     = $fName;
    $GLOBALS['lastname']      = $lName;
    $GLOBALS['email_address'] = $mosUser['email'];
  }
  
  $GLOBALS['country'] = STORE_COUNTRY;
  
if (USE_JOOMLA_CB == 'true'){
	 $cb_field_query = tep_db_query("select * from " . TABLE_COMMPROFILER . " where user_id = '" . $mosUser['id'] . "'");
    $CB_fields_desc = tep_db_fetch_array($cb_field_query );
    $GLOBALS['street_address'] = $CB_fields_desc['address'];
    $CBGender = $CB_fields_desc['cb_gender'];
   			if ($CBGender == 'Female') {
			$GLOBALS['gender'] = 'f';
			} else {
			$GLOBALS['gender'] = 'm';
			}
	$GLOBALS['company'] = $CB_fields_desc['company'];
	$GLOBALS['city'] = $CB_fields_desc['city'];	
	$CBCntry = $CB_fields_desc['country'];
		if ($CBCntry != ''){
		$cb_ccode_query = tep_db_query("select * from " . TABLE_COUNTRIES . " where countries_name = '" . $CBCntry . "'");
    $CB_ccode_desc = tep_db_fetch_array($cb_ccode_query );
    $GLOBALS['country'] = $CB_ccode_desc['countries_id'];
    }
    $GLOBALS['state'] = $CB_fields_desc['state'];
    $GLOBALS['postcode'] = $CB_fields_desc['zipcode'];
	$GLOBALS['telephone'] = $CB_fields_desc['phone'];
	$GLOBALS['fax'] = $CB_fields_desc['fax'];
		}
		
		
  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL'));
 require('includes/form_check.js.php');
 $a['form'] = tep_draw_form('create_account', tep_href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL'), 'post', 'onSubmit="return check_form(create_account);"') . tep_draw_hidden_field('action', 'process');
 if($_REQUEST['backto'] != ''){
 //$a['form'] .= tep_draw_hidden_field('backto', $_REQUEST['backto']);
 }
 $a['heading'] = HEADING_TITLE;
 $a['head_img'] = tep_image(DIR_WS_IMAGES . 'table_background_account.gif', HEADING_TITLE, HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT);
 
$a['required'] = FORM_REQUIRED_INFORMATION;
if (!$mosUser['validated']) { 
$a['intro'] = sprintf(TEXT_ORIGIN_LOGIN, tep_href_link(FILENAME_LOGIN, tep_get_all_get_params(), 'SSL'));
 } else { 
$a['intro'] = TEXT_UPDATE_ACCOUNT;  
} 
if ($messageStack->size('create_account') > 0) {
$a['messagestack'] =  $messageStack->output('create_account'); 
  }
  $a['usernameinfo'] = CATEGORY_PASSWORD;
  $a['username'] = ENTRY_USERNAME; 
  $a['username_input'] = tep_draw_input_field('username') . '&nbsp;' . (tep_not_null(ENTRY_USERNAME_TEXT) ? '<span class="inputRequirement">' . ENTRY_USERNAME_TEXT . '</span>': '');
  $a['password'] = ENTRY_PASSWORD;
  $a['password_input'] = tep_draw_password_field('password') . '&nbsp;' . (tep_not_null(ENTRY_PASSWORD_TEXT) ? '<span class="inputRequirement">' . ENTRY_PASSWORD_TEXT . '</span>': '');
  $a['password_conf'] = ENTRY_PASSWORD_CONFIRMATION;
  $a['password_conf_input'] = tep_draw_password_field('confirmation') . '&nbsp;' . (tep_not_null(ENTRY_PASSWORD_CONFIRMATION_TEXT) ? '<span class="inputRequirement">' . ENTRY_PASSWORD_CONFIRMATION_TEXT . '</span>': '');
  $a['personalinfo'] = CATEGORY_PERSONAL;
  $a['firstname'] = ENTRY_FIRST_NAME;
  $a['firstname_input'] = tep_draw_input_field('firstname',$GLOBALS['firstname']) . '&nbsp;' . (tep_not_null(ENTRY_FIRST_NAME_TEXT) ? '<span class="inputRequirement">' . ENTRY_FIRST_NAME_TEXT . '</span>': '');
  $a['lastname'] = ENTRY_LAST_NAME;
  $a['lastname_input'] = tep_draw_input_field('lastname', $GLOBALS['lastname']) . '&nbsp;' . (tep_not_null(ENTRY_LAST_NAME_TEXT) ? '<span class="inputRequirement">' . ENTRY_LAST_NAME_TEXT . '</span>': '');
  $a['dateofbirth'] = ENTRY_DATE_OF_BIRTH;
  for ($i=1; $i<32; $i++) {
			$dob_day_arr[] = array('id' => sprintf('%02d', $i), 'text' => sprintf('%02d', $i));
		}

		for ($i=1; $i<13; $i++) {
			$dob_month_arr[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000)));
		}

		$today = getdate();
		$first_year = $today['year'] - 17; 
		$last_year = $today['year'] - 87;
		for ($i=$first_year; $i > $last_year; $i--) {
			$dob_year_arr[] = array('id' => sprintf('%02d', $i), 'text' => sprintf('%02d', $i));
		}

		$a['dateofbirth_input'] =  tep_draw_pull_down_menu('dob_day', $dob_day_arr);
		$a['dateofbirth_input'] .=  tep_draw_pull_down_menu('dob_month', $dob_month_arr);
		$a['dateofbirth_input'] .=  tep_draw_pull_down_menu('dob_year', $dob_year_arr);
		$a['dateofbirth_input'] .=  tep_not_null(ENTRY_DATE_OF_BIRTH_TEXT) ? '<span class="inputRequirement"></span>': '';
		$a['emailadd'] = ENTRY_EMAIL_ADDRESS;
		$a['emailadd_input'] = tep_draw_input_field('email_address', $my->email) . '&nbsp;' . (tep_not_null(ENTRY_EMAIL_ADDRESS_TEXT) ? '<span class="inputRequirement">' . ENTRY_EMAIL_ADDRESS_TEXT . '</span>': '');
		$a['gender'] = ENTRY_GENDER;
		$a['gender_input'] = tep_draw_radio_field('gender', 'm') . '&nbsp;&nbsp;' . MALE . '&nbsp;&nbsp;' . tep_draw_radio_field('gender', 'f') . '&nbsp;&nbsp;' . FEMALE . '&nbsp;' . (tep_not_null(ENTRY_GENDER_TEXT) ? '<span class="inputRequirement">' . ENTRY_GENDER_TEXT . '</span>': '');
		$a['companyinfo'] = CATEGORY_COMPANY;
		$a['company'] = ENTRY_COMPANY;
		$a['company_input'] = tep_draw_input_field('company') . '&nbsp;' . (tep_not_null(ENTRY_COMPANY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COMPANY_TEXT . '</span>': '');
		$a['taxno'] = ENTRY_COMPANY_TAX_ID;
		$a['taxno_input'] = tep_draw_input_field('company_tax_id') . '&nbsp;' . (tep_not_null(ENTRY_COMPANY_TAX_ID_TEXT) ? '<span class="inputRequirement">' . ENTRY_COMPANY_TAX_ID_TEXT . '</span>': '');
		$a['addressinfo'] = CATEGORY_ADDRESS; 
		$a['address'] = ENTRY_STREET_ADDRESS;
		$a['address_input'] = tep_draw_input_field('street_address') . '&nbsp;' . (tep_not_null(ENTRY_STREET_ADDRESS_TEXT) ? '<span class="inputRequirement">' . ENTRY_STREET_ADDRESS_TEXT . '</span>': '');
		$a['suburb'] = ENTRY_SUBURB;
		$a['suburb_input'] = tep_draw_input_field('suburb') . '&nbsp;' . (tep_not_null(ENTRY_SUBURB_TEXT) ? '<span class="inputRequirement">' . ENTRY_SUBURB_TEXT . '</span>': '');
		$a['city'] = ENTRY_CITY;
		$a['city_input'] = tep_draw_input_field('city') . '&nbsp;' . (tep_not_null(ENTRY_CITY_TEXT) ? '<span class="inputRequirement">' . ENTRY_CITY_TEXT . '</span>': '');
		$a['country'] = ENTRY_COUNTRY;
		$a['country_input'] = tep_get_country_list('country', $GLOBALS['country'], 'onchange="loadXMLDoc(this.value);" style="width:150px" ')  . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': '');
		$a['state'] = ENTRY_STATE;
	
    if ($process == true) {
      if ($entry_state_has_zones == true) {
        $zones_array = array();
        $zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
        while ($zones_values = tep_db_fetch_array($zones_query)) {
          $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
        }
        $a['state_input'] .= tep_draw_pull_down_menu('state', $zones_array);
      } else {
        $a['state_input'] .= tep_draw_input_field('state');
      }
    } else {
    $zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . STORE_COUNTRY . "' order by zone_name");
    $zones_array = array();
          while ($zones_values = tep_db_fetch_array($zones_query)) {
            $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
          }
          if (count($zones_array) == 0) {
    
     $a['state_input'] .= tep_draw_input_field('state');
     
     } else {
     
     $a['state_input'] .= tep_draw_pull_down_menu('state', $zones_array);
     }
    }

    if (tep_not_null(ENTRY_STATE_TEXT)) {
    $a['state_input'] .=  '&nbsp;<span class="inputRequirement">' . ENTRY_STATE_TEXT;
    }
    $a['postal'] = ENTRY_POST_CODE;
    $a['postal_input'] = tep_draw_input_field('postcode') . '&nbsp;' . (tep_not_null(ENTRY_POST_CODE_TEXT) ? '<span class="inputRequirement">' . ENTRY_POST_CODE_TEXT . '</span>': '');
    $a['contactinfo'] = CATEGORY_CONTACT;
    $a['telephone'] = ENTRY_TELEPHONE_NUMBER;
    $a['telepone_input'] = tep_draw_input_field('telephone') . '&nbsp;' . (tep_not_null(ENTRY_TELEPHONE_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_TELEPHONE_NUMBER_TEXT . '</span>': '');
    $a['fax'] = ENTRY_FAX_NUMBER;
    $a['fax_input'] =  tep_draw_input_field('fax') . '&nbsp;' . (tep_not_null(ENTRY_FAX_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_FAX_NUMBER_TEXT . '</span>': ''); 
    $a['other'] = CATEGORY_OPTIONS;
    $a['newsletter'] = ENTRY_NEWSLETTER;
    $a['newsletter_input'] = tep_draw_checkbox_field('newsletter', '1') . '&nbsp;' . (tep_not_null(ENTRY_NEWSLETTER_TEXT) ? '<span class="inputRequirement">' . ENTRY_NEWSLETTER_TEXT . '</span>': '');

	$a['button_back'] = tep_image_submit_ECJ('button_back.gif',IMAGE_BUTTON_BACK,IMAGE_BUTTON_BACK,tep_href_link(FILENAME_ACCOUNT, '', 'SSL')); 
	if (isset($_REQUEST['guest'])) {
	$a['button_submit'] = tep_draw_hidden_field('guest',$_REQUEST['guest']);
	}
	$a['button_submit'] .= tep_image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE); 

require(WS_TEMPLATES. E_TEMPLATE_FOLDER . '/'.$osMod.'.html');
require(DIR_WS_INCLUDES . 'footer.php'); 
  require(DIR_WS_INCLUDES . 'application_bottom.php');
  
  

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
  
 ?>